<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理</title>
    <link rel="stylesheet" href="user.css">
</head>
<body>
    <h1>用户个人信息</h1>
    <div id="data-container"></div>
<form action="/register" method="post" name="register">
    <div id="register-box">
        <h1>添加新用户</h1>
        <div class="form">
            <!-- 用户名输入框 -->
            <div class="item">
                <i class="fa fa-user-circle-o" aria-hidden="true"></i>
                <input type="text" name="username" placeholder="Username" id="username" maxlength="50" required>
            </div>
            <!-- 密码输入框 -->
            <div class="item">
                <i class="fa fa-key" aria-hidden="true"></i>
                <input type="password" name="password" placeholder="Password" id="password" maxlength="20" required>
            </div>
            <!-- 确认密码输入框 -->
            <div class="item">
                <i class="fa fa-repeat" aria-hidden="true"></i>
                <input type="password" name="confirm_password" placeholder="Confirm Password" id="confirm_password" maxlength="20" required>
            </div>
            <!-- 邮箱输入框（可选填） -->
            <div class="item">
                <i class="fa fa-envelope-o" aria-hidden="true"></i>
                <input type="email" name="email" placeholder="Email (optional)" id="email" maxlength="100">
            </div>
            <div class="item">
                <input type="tel" name="phone" placeholder="Phone Number (optional)" id="phone" maxlength="15" pattern="[0-9]{10,15}">
            </div>
            <!-- 权限等级下拉框 -->
            <div class="item">
                <p>选择权限等级 (0: 超级管理员, 1: 管理员, 2: 普通用户):</p>
                <select name="role" id="role" required>
                    <option value="">请选择权限等级</option>
                    <option value="0">超级管理员</option>
                    <option value="1">管理员</option>
                    <option value="2">普通用户</option>
                </select>
            </div>
        </div>
        <button type="submit">注册</button>
    </div>
</form>
<br><br><button onclick="navigateToLink1()">菜单栏</button>
</body>
<script>
        // 定义一个函数来执行Ajax请求
        function fetchData() {
            const xhr = new XMLHttpRequest(); // 创建一个新的XMLHttpRequest对象
            xhr.open('POST', '/data', true); // 配置请求的类型、URL和是否异步

            // 设置请求完成后的回调函数
            xhr.onload = function() {
                if (xhr.status >= 200 && xhr.status < 300) {
                    // 解析返回的JSON数据
                    const data = JSON.parse(xhr.responseText);

                    // 获取要显示数据的HTML元素
                    const dataContainer = document.getElementById('data-container');

                    // 动态创建HTML内容并添加到元素中
                    let content = `<p>Id: ${data.Id}</p>`;
                    content += `<p>Username: ${data.Username}</p>`;
                    content += `<p>Email: ${data.Email}</p>`;
                    content += `<p>Phone: ${data.Phone}</p>`; // 假设返回的JSON中包含phone字段
                    content += `<p>Role: ${data.Role}</p>`;

                    dataContainer.innerHTML = content;
                } else {
                    console.error('请求失败，状态码：', xhr.status);
                }
            };

            // 发送请求
            xhr.send();
        }

        // 调用函数以在页面加载时获取数据
        window.onload = fetchData;

        //  回到菜单页
    function navigateToLink1() {
            window.location.href = "http://127.0.0.1:8080/static/menu.html";
        }
</script>
</html>